﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnlamp.DBUtility;//Please add references
namespace jnlamp.DAL
{
	/// <summary>
	/// 数据访问类:scdInfo
	/// </summary>
	public partial class scdInfo
	{
		public scdInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("scdInfo_id", "scdInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int scdInfo_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from scdInfo");
			strSql.Append(" where scdInfo_id=@scdInfo_id");
			SqlParameter[] parameters = {
					new SqlParameter("@scdInfo_id", SqlDbType.Int,4)
			};
			parameters[0].Value = scdInfo_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnlamp.Model.scdInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into scdInfo(");
			strSql.Append("gprs_id,scd_id,dcb_v,xdc_v,dcb_i,scd_i,dcb_s,scd_s,scd_t,vf,inputTime)");
			strSql.Append(" values (");
			strSql.Append("@gprs_id,@scd_id,@dcb_v,@xdc_v,@dcb_i,@scd_i,@dcb_s,@scd_s,@scd_t,@vf,@inputTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@gprs_id", SqlDbType.VarChar,50),
					new SqlParameter("@scd_id", SqlDbType.VarChar,50),
					new SqlParameter("@dcb_v", SqlDbType.Decimal,9),
					new SqlParameter("@xdc_v", SqlDbType.Decimal,9),
					new SqlParameter("@dcb_i", SqlDbType.Bit,1),
					new SqlParameter("@scd_i", SqlDbType.Bit,1),
					new SqlParameter("@dcb_s", SqlDbType.Int,4),
					new SqlParameter("@scd_s", SqlDbType.Int,4),
					new SqlParameter("@scd_t", SqlDbType.Int,4),
					new SqlParameter("@vf", SqlDbType.Int,4),
					new SqlParameter("@inputTime", SqlDbType.DateTime)};
			parameters[0].Value = model.gprs_id;
			parameters[1].Value = model.scd_id;
			parameters[2].Value = model.dcb_v;
			parameters[3].Value = model.xdc_v;
			parameters[4].Value = model.dcb_i;
			parameters[5].Value = model.scd_i;
			parameters[6].Value = model.dcb_s;
			parameters[7].Value = model.scd_s;
			parameters[8].Value = model.scd_t;
			parameters[9].Value = model.vf;
			parameters[10].Value = model.inputTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnlamp.Model.scdInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update scdInfo set ");
			strSql.Append("gprs_id=@gprs_id,");
			strSql.Append("scd_id=@scd_id,");
			strSql.Append("dcb_v=@dcb_v,");
			strSql.Append("xdc_v=@xdc_v,");
			strSql.Append("dcb_i=@dcb_i,");
			strSql.Append("scd_i=@scd_i,");
			strSql.Append("dcb_s=@dcb_s,");
			strSql.Append("scd_s=@scd_s,");
			strSql.Append("scd_t=@scd_t,");
			strSql.Append("vf=@vf,");
			strSql.Append("inputTime=@inputTime");
			strSql.Append(" where scdInfo_id=@scdInfo_id");
			SqlParameter[] parameters = {
					new SqlParameter("@gprs_id", SqlDbType.VarChar,50),
					new SqlParameter("@scd_id", SqlDbType.VarChar,50),
					new SqlParameter("@dcb_v", SqlDbType.Decimal,9),
					new SqlParameter("@xdc_v", SqlDbType.Decimal,9),
					new SqlParameter("@dcb_i", SqlDbType.Bit,1),
					new SqlParameter("@scd_i", SqlDbType.Bit,1),
					new SqlParameter("@dcb_s", SqlDbType.Int,4),
					new SqlParameter("@scd_s", SqlDbType.Int,4),
					new SqlParameter("@scd_t", SqlDbType.Int,4),
					new SqlParameter("@vf", SqlDbType.Int,4),
					new SqlParameter("@inputTime", SqlDbType.DateTime),
					new SqlParameter("@scdInfo_id", SqlDbType.Int,4)};
			parameters[0].Value = model.gprs_id;
			parameters[1].Value = model.scd_id;
			parameters[2].Value = model.dcb_v;
			parameters[3].Value = model.xdc_v;
			parameters[4].Value = model.dcb_i;
			parameters[5].Value = model.scd_i;
			parameters[6].Value = model.dcb_s;
			parameters[7].Value = model.scd_s;
			parameters[8].Value = model.scd_t;
			parameters[9].Value = model.vf;
			parameters[10].Value = model.inputTime;
			parameters[11].Value = model.scdInfo_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int scdInfo_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from scdInfo ");
			strSql.Append(" where scdInfo_id=@scdInfo_id");
			SqlParameter[] parameters = {
					new SqlParameter("@scdInfo_id", SqlDbType.Int,4)
			};
			parameters[0].Value = scdInfo_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string scdInfo_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from scdInfo ");
			strSql.Append(" where scdInfo_id in ("+scdInfo_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnlamp.Model.scdInfo GetModel(int scdInfo_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 scdInfo_id,gprs_id,scd_id,dcb_v,xdc_v,dcb_i,scd_i,dcb_s,scd_s,scd_t,vf,inputTime from scdInfo ");
			strSql.Append(" where scdInfo_id=@scdInfo_id");
			SqlParameter[] parameters = {
					new SqlParameter("@scdInfo_id", SqlDbType.Int,4)
			};
			parameters[0].Value = scdInfo_id;

			jnlamp.Model.scdInfo model=new jnlamp.Model.scdInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnlamp.Model.scdInfo DataRowToModel(DataRow row)
		{
			jnlamp.Model.scdInfo model=new jnlamp.Model.scdInfo();
			if (row != null)
			{
				if(row["scdInfo_id"]!=null && row["scdInfo_id"].ToString()!="")
				{
					model.scdInfo_id=int.Parse(row["scdInfo_id"].ToString());
				}
				if(row["gprs_id"]!=null)
				{
					model.gprs_id=row["gprs_id"].ToString();
				}
				if(row["scd_id"]!=null)
				{
					model.scd_id=row["scd_id"].ToString();
				}
				if(row["dcb_v"]!=null && row["dcb_v"].ToString()!="")
				{
					model.dcb_v=decimal.Parse(row["dcb_v"].ToString());
				}
				if(row["xdc_v"]!=null && row["xdc_v"].ToString()!="")
				{
					model.xdc_v=decimal.Parse(row["xdc_v"].ToString());
				}
				if(row["dcb_i"]!=null && row["dcb_i"].ToString()!="")
				{
					if((row["dcb_i"].ToString()=="1")||(row["dcb_i"].ToString().ToLower()=="true"))
					{
						model.dcb_i=true;
					}
					else
					{
						model.dcb_i=false;
					}
				}
				if(row["scd_i"]!=null && row["scd_i"].ToString()!="")
				{
					if((row["scd_i"].ToString()=="1")||(row["scd_i"].ToString().ToLower()=="true"))
					{
						model.scd_i=true;
					}
					else
					{
						model.scd_i=false;
					}
				}
				if(row["dcb_s"]!=null && row["dcb_s"].ToString()!="")
				{
					model.dcb_s=int.Parse(row["dcb_s"].ToString());
				}
				if(row["scd_s"]!=null && row["scd_s"].ToString()!="")
				{
					model.scd_s=int.Parse(row["scd_s"].ToString());
				}
				if(row["scd_t"]!=null && row["scd_t"].ToString()!="")
				{
					model.scd_t=int.Parse(row["scd_t"].ToString());
				}
				if(row["vf"]!=null && row["vf"].ToString()!="")
				{
					model.vf=int.Parse(row["vf"].ToString());
				}
				if(row["inputTime"]!=null && row["inputTime"].ToString()!="")
				{
					model.inputTime=DateTime.Parse(row["inputTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select scdInfo_id,gprs_id,scd_id,dcb_v,xdc_v,dcb_i,scd_i,dcb_s,scd_s,scd_t,vf,inputTime ");
			strSql.Append(" FROM scdInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" scdInfo_id,gprs_id,scd_id,dcb_v,xdc_v,dcb_i,scd_i,dcb_s,scd_s,scd_t,vf,inputTime ");
			strSql.Append(" FROM scdInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM scdInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.scdInfo_id desc");
			}
			strSql.Append(")AS Row, T.*  from scdInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCountBase(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Vlampinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString(),0);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPageBase(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.gwid desc");
            }
            strSql.Append(")AS Row, T.*  from Vbaselamp T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString(),0);
        }        

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "scdInfo";
			parameters[1].Value = "scdInfo_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListLampId(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct scd_id,scd_id ");
            strSql.Append(" FROM scdInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}

